//
//  LoginViewController.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/10/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "LoginViewController.h"
#import "User.h"
#import "HRStore.h"
#import "Encrypt.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender {
    NSString *uid = [userId text];
    NSString *pwd = [password text];
    BOOL loginSuccess = NO;
    
    User *u = [[HRStore sharedStore] findUserById:uid];
    if (u) {
        NSString *dbPWD = [u password];
        
        
        loginSuccess = [Encrypt isHashMatchedFrom:pwd to:dbPWD];
        if (loginSuccess) {
            extern BOOL isLogin;
            isLogin = YES;
            [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
        }
    }
}
@end
